package com.bj.dz.service.impl;

import com.bj.dz.entity.Users;
import com.bj.dz.mapper.UsersMapper;
import com.bj.dz.service.UsersService;
import com.bj.dz.util.Constants;
import com.bj.dz.util.EmptyUtils;
import com.bj.dz.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UsersServiceImpl implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    public Users getUsersById(Integer id){
        return usersMapper.getUsersById(id);
    }

    public List<Users>	getUsersListByMap(Map<String,Object> param){
        return usersMapper.getUsersListByMap(param);
    }

    public Integer getUsersCountByMap(Map<String,Object> param){
        return usersMapper.getUsersCountByMap(param);
    }

    @Transactional
    public boolean addUsers(Users users){
            return usersMapper.insertUsers(users)>0?true:false;
    }

    @Transactional
    public boolean modifyUsers(Users users){
        return usersMapper.updateUsers(users)>0?true:false;
    }

    @Transactional
    public boolean deleteUsersById(Integer id){
        return usersMapper.deleteUsersById(id)>0?true:false;
    }

    public Page<Users> queryUsersPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize){
        Integer total = usersMapper.getUsersCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Users> usersList = usersMapper.getUsersListByMap(param);
        page.setRows(usersList);
        return page;
    }

}
